<?php

class BasketController extends Core_Controller_Action 
{	
	public function init() {
		
	}
	
	public function indexAction() {
		$this->view->title = 'basket';
		$basket = new Zend_Session_Namespace( 'basket' );
		
		$prod = new Products();
		
		//lista produktów do wyświetlenia
		$list = array();
		if(count($basket->list)){
			foreach($basket->list as $key => $val){
				$list[$key] = $prod->getWithName( $key , $this->locale )->getFirst()->toArray();
			}
		}
		$this->view->basket = $basket->list;
		$this->view->list 	= $list;
	}
	public function remAction(){
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$basket = new Zend_Session_Namespace( 'basket' );
		
		$id 		= $this->_request->getParam( 'id', 0);
		$rozmiar 	= $this->_request->getParam( 'size', 0);
		
		if(isSet($basket->list[$id][$rozmiar])){
			$count = $basket->list[$id][$rozmiar];
			$basket->count -= $count;
			unset($basket->list[$id][$rozmiar]);
			if(!count($basket->list[$id])){
				unset($basket->list[$id]);
			}
		}
		$this->_redirect( 'basket' );
	}
	public function shipAction(){
		$this->view->title = 'shipment';
		$prod = new Products();
		
		$this->view->basket = $prod->getTransport($this->locale)->toArray();
	}
	public function pay2Action(){
		if( $this->_request->isPost() ) {
			$this->_helper->layout->disableLayout();
			$this->_helper->viewRenderer->setNoRender();
			if(!isSet($this->userData->u_id)){
				$this->_redirect( 'basket' );
			}
			$basket_ses		= new Zend_Session_Namespace( 'basket' );
			
			$produkt		= new Products();
			$transaction 	= new Transactions() ;
			$users			= new Users();
			$currency		= new Currency();
			
			$cur			= $currency->getLocal( $this->locale )->getFirst();
			$user			= $users->getItem($this->userData->u_id)->getFirst();
			
			$amount 		= 0;
			
			
			$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
			$conn->beginTransaction();
			try{
				$transaction->date_add = date("Y-m-d H:i:s");
				$transaction->email 			= $user->email;
				$transaction->first_name		= $user->first_name;
				$transaction->last_name			= $user->last_name;
				$transaction->adress 			= $user->adress;
				$transaction->post_code			= $user->post_code;
				$transaction->city				= $user->city;
				$transaction->country			= $user->country;
				$transaction->phone				= $user->phone;
				$transaction->phone2			= $user->phone2;
				$transaction->users_u_id		= $user->u_id;
				$transaction->currency_cur_id	= $cur->cur_id;
				$transaction->save();
				
				foreach($basket_ses->list as $key => $val){
					$one_prod = $produkt->getItemDD($key)->getFirst();
					$price = unserialize($one_prod->price);
					foreach($val as $size => $count){
						$name = 'size_'.$size;
						$one_prod->$name -= $count;
						$amount += $price[$cur->name]*$count;
						
						$basket = new Basket();
						$basket->transactions_tr_id 	= $transaction->tr_id;  
						$basket->products_p_id		 	= $one_prod->p_id;   
						$basket->item				 	= $count;    
						$basket->size				 	= $size;  
						$basket->price				 	= $price[$cur->name];  
						$basket->save();
					}
					
					$one_prod->save();
				}
				$one_prod = $produkt->getItemDD($this->_request->getParam( 'ship', 0))->getFirst();
				$price = unserialize($one_prod->price);
				$amount += $price[$cur->name];
				$basket = new Basket();
				$basket->transactions_tr_id 	= $transaction->tr_id;  
				$basket->products_p_id		 	= $this->_request->getParam( 'ship', 0);;  
				$basket->item				 	= 1;    
				$basket->size				 	= 1;  
				$basket->price				 	= $price[$cur->name];  
				$basket->save();
				
				$transaction->amount	= $amount;
				$transaction->save();
				
				$conn->commit();
				unset($basket_ses->list);
				unset($basket_ses->count);
				
				//wysyłka maila
				$mail = new Core_Mail();
				$mail->addTo($user->email, $user->first_name.' '.$user->last_name );
				$mail->setFrom( 'no-reply@airblaster.pl', 'Airblaster.pl' );
				$mail->setSubject( $this->view->translate("mail_sub"));
				$mail->setBodyHtml( "Witaj, złożyłeś zamówienie na łączną kwotę ". $amount );
				$mail->send();
				
				$this->_redirect( '/basket/pay' );
			}catch(Exception $e) {
				$conn->rollback();	
			}
		}
		else{
			$this->_redirect( 'basket' );
		}
		
	}
	public function payAction(){
		$this->view->title = 'paid';
	}
	
	public function addAction () {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$basket = new Zend_Session_Namespace( 'basket' );
		
		$id 		= $this->_request->getParam( 'prod_id', 0);
		$rozmiar 	= $this->_request->getParam( 'rozmiar', 0);
		$count	 	= $this->_request->getParam( 'count', 0);
		
		if(isSet($basket->list[$id][$rozmiar])){
			$basket->list[$id][$rozmiar] +=$count;
		}
		else{
			$basket->list[$id][$rozmiar] = $count;	
		}
		$basket->count += $count;
		$this->_redirect( 'basket' );
	}
	
	public function thereturnofthekingAction(){
		if ($this->_request->isPost()) {
			$id = $this->_request->getPost('control');
			Core_Logs::log( 'cataclysm', 'dotpay', "URLC OPEN TR_ID: ".$id);
			$uid = $this->_request->getPost('id');
			$transactiondql = new Transactions();
			$transaction = $transactiondql->getItem($id);
			$t_status = $this->_request->getPost('t_status');
			$t_id = $this->_request->getPost('t_id');
			$service = $this->_request->getPost('service');
			$code = $this->_request->getPost('code');
			$username = $this->_request->getPost('username');
			$kanal = $this->_request->getPost('channel', 0);
			$password = $this->_request->getPost('password');
			$oa = $this->_request->getPost('orginal_amount');
			$waluta = substr($oa, strlen($oa)-3, strlen($oa));
			$waltmp = new Currency();
			$waltmp2 = $waltmp->getItem($transaction[0]['currency_cur_id']);
			$waluta2 = $waltmp2[0]['name'];
			$amount = number_format($transaction[0]['amount'], 2, '.', ' ');
			$md5c = md5("mnbvcxzasdfghjkl:".$uid.":".$id.":".$t_id.":".$amount.":".$transaction[0]['email'].":".$service.":".$code.":".$username.":".$password.":".$t_status);
			$md5 = $this->_request->getPost('md5');
//			var_dump($md5c);
//			exit;
			if($md5 == $md5c){
				Core_Logs::log( 'cataclysm', 'dotpay', "URLC MD5: OK");
				switch ($t_status){
					case 1:
						Core_Logs::log( 'cataclysm', 'dotpay', "URLC STATUS: NEW");
						die('OK');
						break;
					case 2:
						if($waluta2 != $waluta){
							Core_Logs::log( 'cataclysm', 'dotpay', "URLC VALUT: ERROR<----------------");
							die('OK');
						}elseif($transaction[0]['paid'] == 'no'){
							$transaction[0]->paid_transaction( $kanal );
							Core_Logs::log( 'cataclysm', 'dotpay', "URLC PAID: OK");
							die('OK');
						}else{
							Core_Logs::log( 'cataclysm', 'dotpay', "URLC PAID: ERROR - its paid!<----------------");
							die('OK');
						}
						break;
					case 3:
						Core_Logs::log( 'cataclysm', 'dotpay', "URLC PAID: NO<----------------");
						die('OK');
						break;
					case 4:
						Core_Logs::log( 'cataclysm', 'dotpay', "URLC STATUS: CANCEL<----------------");
						die('OK');
						break;
					case 5:
						Core_Logs::log( 'cataclysm', 'dotpay', "URLC STATUS: RECLAMATION<----------------");
						die('OK');
						break;
				}
			}else{
				Core_Logs::log( 'cataclysm', 'dotpay', "URLC MD5: ERROR<----------------");
				die('OK');
			}
		}else{
			$ip = $this->_request->getClientIp();
			Core_Logs::log( 'cataclysm', 'dotpay', "Kernel Panic!<----------------\nIP: ".$ip." -> GET");
			$this->_redirect( 'index' );
		}
	}
}

